#pragma once
#include <assert.h>
#include <algorithm>

using namespace std;

template<class T>
struct List_Node 
{
	List_Node<T>* _next;
	List_Node<T>* _prev;
	T _data;

	List_Node(const T& val = T())
		:_next(nullptr)
		,_prev(nullptr)
		,_data(val)
	{}
};

template <class T,class Ref,class Ptr>
struct  _List_Iterator
{
	typedef List_Node<T> Node;
	typedef _List_Iterator<T, Ref, Ptr> self;
	Node* _node;

	_List_Iterator(Node* node)
		:_node(node)
	{}

	Ref& operator*()
	{
		return _node->_data;
	}

	Ptr* operator ->()
	{
		return &(_node->_data);
	}

	self operator++()
	{
		_node = _node->_next;
		return _node;
	}

	self operator++(int)
	{
		self tmp(*this);
		_node = _node->_next;
		return tmp;
	}

	self operator--()
	{
		_node = _node->_prev;
		return _node;
	}

	self operator--(int)
	{
		self* tmp(*this);
		_node = _node->_prev;
		return tmp;
	}

	bool operator==(const self& it)
	{
		return _node == it._node;
	}

	bool operator!=(const self& it)
	{
		return _node != it._node;
	}
};

template <class T>
class List {
	typedef List_Node<T> Node;
	typedef _List_Iterator<T, T&, T*> iterator;
	typedef _List_Iterator<T, const T&,const T*> const_iterator;

public:
	List()
	{
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;
	}

	const_iterator begin() const
	{
		return const_iterator(_head->_next);
	}

	iterator begin() 
	{
		return iterator(_head->_next);
	}

	iterator end() 
	{
		return iterator(_head);
	}

	const_iterator end() const
	{
		return const_iterator(_head);
	}
	
	void emptyinit()
	{
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;
	}

	template <class InputIterator>
	List(InputIterator start, InputIterator finish)
	{
		emptyinit();
		while (start != finish)
		{
			push_back(*start);
			start++;
		}
	}

	void swap(List<T>& lt)
	{
		std::swap(_head, lt._head);
	}

	List(const List<T>& lt)
	{
		emptyinit();
		List<T> tmp(lt.begin(), lt.end());
		swap(tmp);
	}

	List<T>& operator = (List<T> lt)
	{
		swap(lt);
		return *this;
	}

	~List()
	{
		clear();
		delete _head;
		_head = nullptr;
	}

	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}

	void push_back(const T& x)
	{
		//Node* newnode = new Node(x);
		//Node* tail = _head->_prev;

		//tail->_next = newnode;
		//newnode->_prev = tail;
		//newnode->_next = _head;
		//_head->_prev = newnode;
		insert(end(), x);
	}

	void push_front(const T& x)
	{
		insert(begin(), x);
	}

	void pop_front()
	{
		erase(begin());
	}

	void pop_back()
	{
		erase(--end());
	}

	iterator insert(iterator pos,const T& x)
	{
		Node* newnode = new Node(x);
		Node* prev = pos._node->_prev;
		Node* cur = pos._node;

		prev->_next = newnode;
		cur->_prev = newnode;
		newnode->_next = cur;
		newnode->_prev = prev;

		return iterator(newnode);
	}

	iterator erase(iterator pos)
	{
		assert(pos != end());

		Node* cur = pos._node;
		Node* next = cur->_next;
		Node* prev = cur->_prev;

		prev->_next = next;
		next->_prev = prev;
		delete cur;

		return iterator(next);
	}
	
private:
	Node* _head;
};

